<?php

// +----------------------------------------------------------------------
// | MC PHP[基于ThinkPHP6.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://www.mingchangruanjian.com
// +----------------------------------------------------------------------
// | Author jokerfei [ jokerfei377@gmail.com ]
// +----------------------------------------------------------------------
// | 铭畅软件 技术部 出品
// +----------------------------------------------------------------------

namespace app\api\controller;

use app\common\model\Api;
use app\common\service\ApiReturn;
use think\Paginator;

// API基础控制器

class Base
{

    // 返回字段名称 数组
    public array $fname;

    public function __construct()
    {
        header('Access-Control-Allow-Origin:*');
        //设置分页参数
        $list_rows = input('list_rows/d', 0);
        if ($list_rows > 0) {
            $list_rows = $list_rows > 100 ? 100 : $list_rows;
            config('paginate.list_rows', $list_rows);
        }

        $hash        = input('hash');
        $data        = \app\common\model\Apifields::getCacheFields($hash, 1);
        $this->fname = [];
        foreach ($data as $v) {
            $this->fname[$v['fieldname']] = [
                'ismust'  => $v['ismust'],
                'default' => $v['default']
            ];
        }
    }

    /**
     * API入口，修改验证规则机制
     * @param string $hash
     * @return
     * @Author jokerfei [ jokerfei377@gmail.com ]
     */
    public function iniApi($version, string $hash)
    {
        $ret = Api::init($hash);
        // 0为正常输出
        if ($ret === 0) {
            ApiReturn::$user = Api::$user;
            try {
                $apinamearr   = explode('/', Api::$apiInfo['apiname']);
                $apiclassname = ucfirst($apinamearr[0]);
                $apiname      = $apinamearr[1];
                return invoke([
                    'app\api\controller\\' . $version . '\\' . $apiclassname,
                    $apiname
                ],
                    [Api::$param, Api::$user]);
            } catch (\Exception $e) {
                return ApiReturn::r($e->getCode(), "", $e->getMessage());
            }
        }
        return $ret;
    }

    /**
     * 数据过滤转换
     * 对FNAME进行了重写，所有字段若未定义按照默认值填入
     * @param $data
     * @param null $whiteList
     * @return array
     * @Author jokerfei [ jokerfei377@gmail.com ]
     */
    public function filter($data, $whiteList = null)
    {
        $whiteList = $whiteList === null ? $this->fname : $whiteList;
        $newData   = array();
        foreach ($whiteList as $key => $val) {
            if (is_array($val)) {
                //若不是非必填且该字段未定义，则忽略
                if (!$val['ismust'] && !isset($data[$key])) {
                    continue;
                }
                $newData[$key] = $data[$key] ?? $val['default']; //检查字段，若不存在则写入默认值
            }
            else {
                $newData[$val] = $data[$val] ?? '';
            }
        }
        return $newData;
    }

    /**
     * 获取TP分页 参数
     * 用例 return ApiReturn::r(1,$info,"获取成功",$this->getThinkPage($list));
     * @param Paginator $paginate 分页查询结果集
     * @return array
     */
    public function getThinkPage(Paginator $paginate)
    {
        $data["total"]        = $paginate->total();
        $data["listRows"]     = $paginate->listRows();
        $data["current_page"] = $paginate->getCurrentPage();
        $data["last_page"]    = $paginate->lastPage();
        return $data;
    }

}
